// These are meant to be typed into the REPL. You can also run
// scala -Xnojline < 01_readline.sc to run them all at once.
/**
  * 第15章文件和正则表达式
  * 15.1读取行
  * 要读取文件中的所有行，可以调用scala.io.Source对象的getLines方法：
  */

import scala.io.Source

val source = Source.fromFile("mary.txt", "UTF-8")
//第一个参数可以字符串或者是 java.io.File
//如果你知道文件实用的是当前平台缺省的字符编码，则可以略去第二个字符编码参数
val lineIterator = source.getLines

for (line <- lineIterator)
  println(if (line.length <= 13) line else line.substring(0, 10) + "...")

source.close()

// Caution: The sources below aren't closed.

val lines = Source.fromFile("mary.txt", "UTF-8").getLines.toArray

//如果想把一个文件读成一个字符串
val contents = Source.fromFile("mary.txt", "UTF-8").mkString


